{% set per_page_size = 11 %}
{% set page_count = [0] %}
{% set op_name = [''] %}
{% set index = [0] %}

<style type="text/css">
.breakpage { page-break-after:always;}
</style>
<table border="1">
<tr>

<td>业务单号</td><td>寄件单位</td><td>寄件人姓名</td><td>寄件人电话</td><td>寄件人手机</td><td>寄件人省</td><td>寄件人市</td><td>寄件区/县</td><td>寄件人地址</td><td>寄件人邮编</td><td>收件人姓名</td><td>收件人电话</td><td>收件人手机</td><td>收件省</td><td>收件市</td><td>收件区/县</td><td>收件人地址</td><td>收件邮政编码</td><td>运费</td><td>订单金额</td><td>商品名称</td><td>商品编码</td><td>销售属性</td><td>商品金额</td><td>数量</td><td>备注</td>
</tr>
{% macro item_tr(index, page_count, td_left_color, td_style, td_top_color, id, mobile, phone, address, name, department, linkname, grade, customer, op_name, province_name, city_name, county_name) -%}
    {% set td_style = '' %}
    {% set td_top_color = '' %}
    {% set td_left_color = '' %}

    {% set page_break = index[0] % (per_page_size * 2) == 0 %}
    {% set last_op_name = op_name[0] %}
    {% if op_name.pop(),op_name.append(customer.op_name) %}
        {% set op_mobile = customer.op_name %}
    {% endif %}

    <!-- 如果业务员名字不同则进行分页 -->
    {% if op_name[0] != last_op_name and last_op_name != '' %}
        {% set halving_line = true %}
        {% set page_break = true %}
        {% if index.pop() %}{% endif %}
        {% if index.append(0) %}{% endif %}
    {% else %}
        {% set halving_line = false %}
    {% endif %}

    {% if halving_line %}
      {% set td_top_color = 'border-top:1px solid #55555;' %}
      {% set td_left_color = 'border-left:1px solid #55555;' %}
    {% endif %}

        <tr>
            <td class="业务单号">{{id}}</td>
            <td class="寄件单位">东方团购中心</td>
            <td class="寄件人姓名">{{op_name[0]}}</td>
            <td class="寄件人电话"></td>
            <td class="寄件人手机">{{customer.op_mobile}}</td>
            <td class="寄件人省">上海</td>
            <td class="寄件人市">上海</td>
            <td class="寄件区/县">杨浦区</td>
            <td class="寄件人地址">国伟路135号10号楼一层</td>
            <td class="寄件人邮编">200438</td>
            <td class="收件人姓名">{{department|default('',True)}} {{linkname|default('',True)}}</td>
            <td class="收件人电话">{{phone|default('',True)}}</td>
            <td class="收件人手机">{{mobile|default('-',True)}}</td>
            <td class="收件省">{{province_name}}</td>
            <td class="收件市">{{city_name}}</td>
            <td class="收件区/县">{{county_name}}</td>
            <td class="收件人地址">{{address|default('',True)}}</td>
            <td class="收件邮政编码"></td>
            <td class="运费"></td>
            <td class="订单金额"></td>
            <td class="商品名称"></td>
            <td class="商品编码"></td>
            <td class="销售属性"></td>
            <td class="商品金额"></td>
            <td class="数量"></td>
            <td class="备注">{{name}}</td>
        </tr>
    {% if index.append(index.pop() + 1) %}{% endif %}
{%- endmacro %}

{% for customer in customers %}
    {% set main_contacts = customer.main_contacts %}
    <!-- 取得需要邮寄的重要联系人 -->
    {% set contacts_rows = _cr.dictfetchall("SELECT rs.id, rs.name, cc.department, rs.province_id, rs.city_id, rs.county_id, rs.street, rs.mobile, rs.phone FROM company_contacts cc INNER JOIN res_partner rs ON cc.contacts_id=rs.id WHERE cc.is_inauguratory=TRUE AND cc.partner_id=%s AND cc.subscription_type='%s'" % (customer.id, subscription_type)) %}
    <!-- 遍历需要打印的重要联系人 -->
    {% for contacts in contacts_rows %}
        {% set id = contacts.id %}
        {% set department = contacts.department %}
        {% set grade = grades[customer.cust_grade] %}
        {% set linkname = contacts.name %}
        {% set address = contacts.street %}
        {% set phone = contacts.phone %}
        {% set mobile = contacts.mobile %}
        {% set province_name = provinces[contacts.province_id] %}
        {% set city_name = cities[contacts.city_id] %}
        {% set county_name = counties[contacts.county_id] %}
        {{ item_tr(index, page_count, td_left_color, td_style, td_top_color, id, mobile, phone, address, customer.name, department, linkname, grade, customer, op_name, province_name, city_name, county_name) }}
    {% endfor %}


{% endfor %}
</table>
</div>